package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.dao.TraderAndStorageDao.SuppliersMapper;
import com.hqyj.pojo.ResultVoH;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.pojo.Suppliers;
import com.hqyj.service.ISuppliersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2022-09-30
 */
@Service
public class SuppliersServiceImpl extends ServiceImpl<SuppliersMapper, Suppliers> implements ISuppliersService {

    @Autowired
    private SuppliersMapper suppliersMapper;
    @Override
    public ResultVoH selectAll() {
        ResultVoH vo = new ResultVoH();
        List<Suppliers> suppliers = suppliersMapper.queryAll();
        vo.setData(suppliers);
        return vo;
    }

    @Override
    public ResultVoH QueryByCondition(Suppliers suppliers) {
        ResultVoH vo = new ResultVoH();
        //创建分页对象
        Page<Suppliers> pages = new Page<>(suppliers.getPage(), suppliers.getRow());
        //创建条件构造器
        QueryWrapper<Suppliers> qw = new QueryWrapper<>();
        qw.like(!suppliers.getSuppliersName().equals("") && suppliers.getSuppliersName() != null,
                "suppliers_name", suppliers.getSuppliersName());
        qw.like(!suppliers.getSuppliersPid().equals("") && suppliers.getSuppliersPid() != null,
                "suppliers_pid", suppliers.getSuppliersPid());

        //查询分页
        Page<Suppliers> pageList = suppliersMapper.selectPage(pages,qw);
        if (pageList.getRecords().size() == 0) {
            vo.setInfo("没有查询到数据");
            return vo;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("totalPage", pageList.getPages());//总页数
        map.put("total", pageList.getTotal());//总条数
        map.put("list", pageList.getRecords());//数据集合

        vo.setSuccess(true);
        vo.setData(map);
        System.out.println(map.values());
        return vo;
    }

    @Override
    public ResultVoH updateByCondition(Suppliers suppliers) {
        ResultVoH vo = new ResultVoH();
        int i = suppliersMapper.updateById(suppliers);
        if(i>0){
            vo.setSuccess(true);
            vo.setInfo("修改成功");
            return vo;
        }else {
            vo.setSuccess(false);
            vo.setInfo("修改失败");
            return vo;
        }
    }

    /**
     * 添加供应商
     * @param suppliers
     * @return
     */
    @Override
    public ResultVoH addSuppliers(Suppliers suppliers) {
        ResultVoH vo = new ResultVoH();
        int i = suppliersMapper.insert(suppliers);
        if(i>0){
            vo.setSuccess(true);
            vo.setInfo("添加成功");
            return vo;
        }
        else {
            vo.setInfo("添加失败");
            vo.setSuccess(false);
            return vo;
        }
    }

    @Override
    public ResultVoH deleteSuppliers(Suppliers suppliers) {
        ResultVoH vo = new ResultVoH();
        int i = suppliersMapper.deleteById(suppliers.getSuppliersId());
        if(i>0){
            vo.setSuccess(true);
            vo.setInfo("删除成功");
            return vo;
        }else {
            vo.setSuccess(false);
            vo.setInfo("删除失败");
            return vo;
        }
    }
}
